<?
class Pager{
	function getPagerData($numHits, $limit, $page){
		$numHits  = (int) $numHits;
		$limit    = max((int) $limit, 1);
		$page     = (int) $page;
		$numPages = ceil($numHits / $limit);
		$page = max($page, 1);
		$page = min($page, $numPages);
		$offset = ($page - 1) * $limit;
		$ret = new stdClass;
		$ret->offset = $offset;
		$ret->limit = $limit;
		$ret->numPages = $numPages;
		$ret->page = $page;
		$ret->page_prev = ($page - 1);
		$ret->page_next = ($page + 1);
		return $ret;
	}
}
class Docso{
	function dochangchuc($so,$daydu){
		$mangso = array('Không','Một','Hai','Ba','Bốn','Năm','Sáu','Bảy','Tám','Chín');
		$chuoi = "";
		$chuc = floor($so/10);//6
		$donvi = $so%10;//1
		if ($chuc>1) {
			$chuoi = " " . $mangso[$chuc] . " Mươi";
			if ($donvi==1) {
				$chuoi .= " Mốt";
			}
			elseif($donvi>1){
				$chuoi .= " " . $mangso[$donvi];
			}
		} 
		elseif ($chuc==1) {
			$chuoi = " Mười";
			if ($donvi==1) {
				$chuoi .= " Một";
			}
			elseif($donvi>1){
				$chuoi .= " " . $mangso[$donvi];
			}
		} 
		elseif ($daydu && $donvi>0) {
			$chuoi = " Lẻ";
		}
		if ($donvi==5 && $chuc>0) {
			$chuoi .= " Lăm";
		} 
		elseif ($donvi>0&&$chuc==0) {
			$chuoi .= " " . $mangso[$donvi];
		}
		return $chuoi;
	}
	function docblock($so,$daydu){
		$mangso = array('Không','Một','Hai','Ba','Bốn','Năm','Sáu','Bảy','Tám','Chín');
		$chuoi = "";
		$tram = floor($so/100);
		$so = $so%100;
		if ($daydu || $tram>0) {
			$chuoi = " " . $mangso[$tram] . " Trăm";
			$chuoi .= Docso::dochangchuc($so,true);
		}
		else {
			$chuoi = Docso::dochangchuc($so,false);
		}
		return $chuoi;
	}
	function dochangtrieu($so,$daydu){
		$chuoi = "";
		$trieu = floor($so/1000000);
		$so = $so%1000000;
		if ($trieu>0) {
			$chuoi = Docso::docblock($trieu,$daydu) . " Triệu";
			$daydu = true;
		}
		$nghin = floor($so/1000);
		$so = $so%1000;
		if ($nghin>0) {
			$chuoi .= Docso::docblock($nghin,$daydu) . " Nghìn";
			$daydu = true;
		}
		if ($so>0) {
			$chuoi .= Docso::docblock($so,$daydu);
		}
		return $chuoi;
	}
	function doc($so){//561000
		$mangso = array('Không','Một','Hai','Ba','Bốn','Năm','Sáu','Bảy','Tám','Chín');
		if ($so==0) return $mangso[0];
		$chuoi = "";
		$hauto = "";
		do {
			$ty = $so%1000000000;
			$so = floor($so/1000000000);
			if ($so>0) {
				$chuoi = Docso::dochangtrieu($ty,true) . $hauto . $chuoi;
			} 
			else {
				$chuoi = Docso::dochangtrieu($ty,false) . $hauto . $chuoi;
			}
			$hauto = " Tỷ";
		} while ($so>0);
		return $chuoi;
	}
}
function arrchunk($arr, $num){
	$arr = substr($arr, 2);
	$arr = explode('~>', $arr);
	$arr = array_chunk($arr, $num);
	return $arr;
}
function str2arr($arr){
	$arr = substr($arr, 2);
	$arr = explode('~>', $arr);
	return $arr;
}
function arr2str($arr){
	foreach($arr as $key => $value){
		$str = $str.",'".$value."'";
	}
	$str = substr($str, 1);
	return $str;
}
function contract_status($status){
	switch($status){
		case 0: return 'Đã hủy'; break;
		case 1: return 'Mới nhập'; break;
		case 2: return 'Đã ký hợp đồng'; break;
		case 3: return 'Đã tạm ứng'; break;
		case 4: return 'Đã giao hàng'; break;
	}
}
function contract_status_class($status){
	switch($status){
		case 0: return 'label arrowed'; break;
		case 1: return 'label label-warning arrowed arrowed-right'; break;
		case 2: return 'label label-danger arrowed'; break;
		case 3: return 'label label-info arrowed-right arrowed-in'; break;
		case 4: return 'label label-success arrowed-in arrowed-in-right'; break;
	}
}
function contract_status_class_btn($status){
	switch($status){
		case 0: return 'btn'; break;
		case 1: return 'btn btn-warning'; break;
		case 2: return 'btn btn-danger'; break;
		case 3: return 'btn btn-info'; break;
		case 4: return 'btn btn-success'; break;
	}
}
function today_str(){
	return date('Y-m-d 00:00:00', time());
}
function tomorrow_str(){
	return date('Y-m-d 00:00:00', (time()+86400));
}
function unit($unit){
	switch($unit){
		case 'quyen50': return 'Quyển'; break;
		case 'quyen100': return 'Quyển'; break;
		case 'so': return 'Số'; break;
		case 'thung': return 'Thùng'; break;
	}
}
function qty_format($quantity){
	switch (strlen($quantity)){
		case 1: //i=1
			return '000000'.$quantity;
			break;
		case 2: //i=2
			return '00000'.$quantity;
			break;
		case 3: //i=3
			return '0000'.$quantity;
			break; 
		case 4: //i=4
			return '000'.$quantity;
			break; 
		case 5:
			return '00'.$quantity;
			break; 
		case 6:
			return '0'.$quantity;
			break; 
		case 7:
			return $quantity;
			break;
	}
}
function bowel($bowel){
	switch ($bowel){
		case 'baibang': return 'Bãi Bằng'; break;
		case 'cacbon': return 'Cacbon'; break;
		case 'offset': return 'Offset'; break;
	}
}
function contract_page($page, $page1, $page2, $page3, $page4){
	$contract_page = '';
	if($page == 1){
		$contract_page = '~>'.$page3.'~>'.$page4.'~>'.($page4 + 1).'~>'.($page4 + 2).'~>'.($page4 + 3);
	}
	elseif($page == 2){
		$contract_page = '~>'.$page2.'~>'.$page3.'~>'.$page4.'~>'.($page4 + 1).'~>'.($page4 + 2);
	}
	elseif($page == $page1){
		$contract_page = '~>'.($page2 - 3).'~>'.($page2 - 2).'~>'.($page2 - 1).'~>'.$page2.'~>'.$page3;
	}
	elseif($page == ($page1 - 1)){
		$contract_page = '~>'.($page2 - 2).'~>'.($page2 - 1).'~>'.$page2.'~>'.$page3.'~>'.$page4;
	}
	else{
		$contract_page = '~>'.($page2 - 1).'~>'.$page2.'~>'.$page3.'~>'.$page4.'~>'.($page4 + 1);
	}
	$contract_page = str2arr($contract_page);
	if($page1 < 5 && $page1 > 0){
		$contract_page = array_chunk($contract_page, $page1);
		return $contract_page[0];
	}
	else{
		return $contract_page;
	}

}
function qty_check($qty_t, $qty_d, $unit){
	if($unit == 'quyen50'){
		if(($qty_t > $qty_d)||($qty_t % 50 != 1)||($qty_d % 50 != 0)){
			return false;
		}
		else{
			return (($qty_d - $qty_t + 1) / 50);
		}
	}
	elseif($unit == 'quyen100'){
		if(($qty_t > $qty_d)||($qty_t % 100 != 1)||($qty_d % 100 != 0)){
			return false;
		}
		else{
			return (($qty_d - $qty_t + 1) / 100);
		}
	}
	else{
		if($qty_t > $qty_d){
			return false;
		}
		else{
			return ($qty_d - $qty_t + 1);
		}
	}
}
function sohuy($p_page, $qty_t, $qty_d){
	$result = round(((($qty_d - $qty_t) + 1) * $p_page) / 40);
	return $result;
}
function banhuy($p_page, $color){
	$result = $p_page + $color - 1;
	return $result;
}
function qty_d($unit, $qty){
	switch($unit){
		case 'quyen50': return $qty * 50; break;
		case 'quyen100': return $qty * 100; break;
		case 'so': return $qty; break;
		case 'thung': return $qty * 500; break;
	}
}
function qty_print($a, $b, $unit){
	$ret = new stdClass;
	switch($unit){
		case 'quyen50': {
			$ret->f = ($a + 49) / 50;
			$ret->t = $b / 50;
			break;
		}
		case 'quyen100': {
			$ret->f = ($a + 99) / 100;
			$ret->t = $b / 100;
			break;
		}
	}
	return $ret;
}
function getCompany($cid){
	$query = mysql_query("SELECT * FROM `contract` WHERE `id` = '$cid'");
	$rs = mysql_fetch_object($query);
	return $rs;
}
function print_cover($i){
	$i1 = $i * 50 - 49;
	$i2 = $i * 50;
	$rs = new stdClass;
	switch (strlen($i1)){
		case 1:
			$rs->sotruoc='000000'.$i1;
			$rs->sosau='00000'.$i2;
			break;
		case 2:
			$rs->sotruoc='00000'.$i1;
			$rs->sosau='0000'.$i2;
			break;
		case 3:
			$rs->sotruoc='0000'.$i1;
			if($i2==1000)
			{
				$rs->sosau='000'.$i2;
			}
			else
			{
				$rs->sosau='0000'.$i2;
			}
			break; 
		case 4:
			$rs->sotruoc='000'.$i1;
			if($i2==10000)
			{
				$rs->sosau='00'.$i2;
			}
			else
			{
				$rs->sosau='000'.$i2;
			}
			break; 
		case 5:
			$rs->sotruoc='00'.$i1;
			if($i2==100000)
			{
				$rs->sosau='0'.$i2;
			}
			else
			{
				$rs->sosau='00'.$i2;
			}
			break; 
		case 6:
			$rs->sotruoc='0'.$i1;
			if($i2==1000000)
			{
				$rs->sosau=''.$i2;
			}
			else
			{
				$rs->sosau='0'.$i2;
			}
			break; 
		case 7:
			$rs->sotruoc=''.$i1;
			$rs->sosau=''.$i2;
			break;
	}
	if($i<10){
		$rs->quyenso='0'.$i;
	}
	else{
		$rs->quyenso = $i;
	}
	return $rs;
}
?>